<html>
<head>
</head>
<body>
<script type="text/javascript" src="http://www.google.com/jsapi">
</script>
<script>
var stato ;
google.load("language", "1");
var xmlhttp;
var site = 'http://memogspread.appspot.com/';
var url = site + 'json/get/item?ss=txY6croxDKSgW6VldBv5m1w&ws=od6'; //French
//var url = site + 'json/get/item?ss=p49h16NQcZpeTuTa-_YZxJg&ws=od6'; //pronto soccorso
// variabili che interessano a browser action 
var item_swapped;
var title;
var an;
var key;
var qu;


//memogspread code
function load_item(url){
	xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	try{
	test = eval( "(" + xmlhttp.responseText + ")" );
	console.log("ritorno da xmlhttp ",test.e);
	if (test.e == 'r1') {
		// send to login page
		// ******** crea un'altra tab e manda un messaggio di errore (cambia il badge in login?)
		console.log("prova ad aprire la tab");
		chrome.tabs.create({url:site+'gadget/index.html'});
		console.log("dopo l'apertura ");
		}
	item_swapped = false;
	if (test.rand) item_swapped = true;
	var text_badge = test.num[0] + '+' + test.num[1];
	title = test.d + ' : ' + text_badge
	chrome.browserAction.setBadgeText ({text: text_badge});
	if (test.num[0]+test.num[1]>0) { //there is sth to repeat
		qu = test.q;
		an = test.a;
		key = test.k;
		stato = 'Item';
		keep_switching_icon = true; //AGGIUNTO PER FARE GIRO ICONE
		}
	else{ //no more repetitions
		qu = 'NO MORE REPS';
		an=key='';
		//set the global variable
		stato = 'Add';
		keep_switching_icon = false; //AGGIUNTO PER FARE GIRO ICONE
		}
	}
	catch (err)	{
		// send to login page
		// ******** crea un'altra tab e manda un messaggio di errore (cambia il badge in login?)
		}
	}

function send(body){
	var req = new XMLHttpRequest();
	var async = false
	req.open('POST', site+'post/item', async);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-type", "application/json");
	req.setRequestHeader("Content-length", body.length);
	req.setRequestHeader("Connection", "close");
	if (async) {
		req.onreadystatechange = function() {
			if(req.readyState == 4 && req.status == 200) {
				var response = null;
				try {
					response = eval( "(" + req.responseText + ")" );
					} 
				catch (e) {
					response = req.responseText;
					}
				}
			}
		}
	req.send(body);
	var a = 1;
	}
	
function onRequest(request, sender, callback) {
	if (request.action == 'getmode') {
		callback(stato);
		}
	if (request.action == 'setmodeadd') {
		stato = "Add";
		}	
  
	if (request.action == 'translate' ) {
		//detect language and translate
		google.load("language", "1");
		google.language.detect(request.text, function(result) {
			var language_dest = 'fr';
			if (result.language == 'fr') language_dest = 'it';
			google.language.translate(request.text, "", language_dest, function(result2) {
				if (!result2.error) callback(result2.translation);
				});
			});	
		};
	if (request.action == 'loaditem' ) {
		if ( request.force ) load_item(url); 
		callback({'q':qu,'a':an,'k':key,'stato':stato,'item_swapped':item_swapped});
		};
	if (request.action == 'send' ) {
		send(request.body);
		an=key=qu='';
		// manca la gestione del return code del send
		callback(true);
		};
	};
//AGGIUNTO PER FARE IL GIRO ICONA
var searching_images = ['France16.png','France48.png','icon.png'];
var keep_switching_icon = true;
var image_index = 0;

chrome.tabs.onRemoved.addListener(function(){
rotateIcon();
}); 


function rotateIcon() {
		if ( keep_switching_icon )
{
		chrome.browserAction.setIcon({path: searching_images[image_index]});
		image_index = (image_index + 1) % searching_images.length;
		window.setTimeout(rotateIcon, 300);
}
 
}

//////
	
// Wire up the listener.
chrome.extension.onRequest.addListener(onRequest);
//carica i risultati per la prima volta
load_item(url);

</script>
</body>
</html>
